<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="addMenu">添加菜单</el-button>
      </div>
      <el-table :data="menus" style="width: 100%">
        <el-table-column type="index" label="编号">
        </el-table-column>
        <el-table-column prop="name" label="菜单名称">
        </el-table-column>
        <el-table-column prop="level" label="菜单级数"> </el-table-column>
        <el-table-column prop="icon" label="前端图标"> </el-table-column>
        <el-table-column prop="orderNum" label="排序"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="edit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="delete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllMenus, deleteMeun } from '@/services/menu'
export default Vue.extend({
  name: 'Menu',
  data () {
    return {
      menus: []
    }
  },
  created () {
    this.loadAllMenus()
  },
  methods: {
    async loadAllMenus () {
      const { data } = await getAllMenus()
      if (data.code === '000000') {
        this.menus = data.data
      }
    },
    addMenu () {
      console.log('menu')
    },
    edit (index: number, item: any) {
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: item.id
        }
      })
    },
    delete (index: number, item: any) {
      this.$confirm('sure? ', 'tips')
        .then(async () => {
          const { data } = await deleteMeun(item.id)
          if (data.code === '000000') {
            this.loadAllMenus()
            this.$message.success('删除成功')
          }
        })
        .catch(err => {
          console.log(err)
          this.$message.info('已取消')
        })
    }
  }
})
</script>

<style lang="scss" scoped>
</style>
